package com.example.onlinereadingsystem.demos.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.onlinereadingsystem.demos.web.dto.domain.Bookshelf;
import com.example.onlinereadingsystem.demos.web.dto.domain.Chapter;
import com.example.onlinereadingsystem.demos.web.dto.domain.Novel;
import com.example.onlinereadingsystem.demos.web.dto.request.AddNovelParam;
import com.example.onlinereadingsystem.demos.web.dto.request.RemoveNovelParam;
import com.example.onlinereadingsystem.demos.web.dto.vo.ChapterContentVo;
import com.example.onlinereadingsystem.demos.web.dto.vo.ChapterItemVo;
import com.example.onlinereadingsystem.demos.web.dto.vo.ChapterListVo;
import com.example.onlinereadingsystem.demos.web.dto.vo.NovelReadingDetailVo;
import com.example.onlinereadingsystem.demos.web.service.*;
import com.example.onlinereadingsystem.demos.web.util.FileReaderUtil;
import com.example.onlinereadingsystem.demos.web.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @Description 小说阅读服务实现类
 * @Date 2024/11/7 15:09
 * @Author ljm
 * @Date 2024/11/7
 */
@Service
public class NovelReadingServiceImpl implements NovelReadingService {
    @Autowired
    private NovelService novelService;
    @Autowired
    private ChapterService chapterService;
    @Autowired
    private NovelKindService novelKindService;
    @Autowired
    private FileReaderUtil fileReaderUtil;
    @Autowired
    private BookshelfService bookShelfService;


    /**
     * @auther ljm
     * @create 2024/11/8
     * @describe 根据id获取章节具体内容
     */
    @Override
    public Result<ChapterContentVo> getChapterContent(String id) throws IOException {
        ChapterContentVo res = new ChapterContentVo();
        Chapter chapter = chapterService.getById(id);
        String url = chapter.getChapterContextUrl();
        String[] content = fileReaderUtil.fileReaderByUrl(url);
        BeanUtils.copyProperties(chapter, res);
        res.setChapterContext(content);
        res.setTitle(novelService.getById(chapter.getNovelId()).getTitle());
        return Result.OK(res);
    }

    /**
     * @auther ljm
     * @create 2024/11/8
     * @describe 获取所有章节id列表
     */
    @Override
    public Result<ChapterListVo> getChapterList(String novelId, String chapterId) {
        ChapterListVo res = new ChapterListVo();
        AtomicInteger currentChapterIndex = new AtomicInteger();
        List<ChapterItemVo> itemList = new ArrayList<>();
        LambdaQueryWrapper<Chapter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Chapter::getNovelId, novelId);
        List<Chapter> chapterList = chapterService.list(queryWrapper);
        chapterList.sort(Comparator.comparing(Chapter::getChapterNum));
        AtomicInteger index = new AtomicInteger(0);
        boolean flag = true;
        for (Chapter chapter : chapterList) {
            ChapterItemVo item = new ChapterItemVo();
            BeanUtils.copyProperties(chapter, item);
            if (flag) {
                if (chapter.getId().equals(chapterId)) {
                    currentChapterIndex.set(index.get());
                    flag = false;
                } else {
                    index.getAndIncrement();
                }
            }
            itemList.add(item);
        }
        res.setChapterList(itemList);
        res.setCurrentChapterIndex(currentChapterIndex.get());
        return Result.OK(res);
    }

}
